<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :page-sizes="[2,4,6,8]"
                :total="page.total"
                layout="total, sizes, prev, pager, next,jumper"
                @size-change="handleSizeChange"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formdata.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formdata.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formdata.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formdata.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
    </div>
    <editDialog ref="editDialog" :show-dialog.sync="showDialog" @editRole="getRoleList" />
    <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
      <!-- 权限是一颗树 -->
      <!-- 将数据绑定到组件上 -->
      <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
      <!-- id作为唯一标识 -->
      <el-tree
        ref="permTree"
        :data="permData"
        :props="defaultProps"
        :show-checkbox="true"
        :check-strictly="true"
        :default-expand-all="true"
        :default-checked-keys="selectCheck"
        node-key="id"
      />
      <!-- 确定 取消 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import editDialog from '@/views/setting/components/editDialog.vue'
import { getCompanyInfo, getRoleList, deleteRole, getRoleDetail } from '@/api/setting'
import { mapGetters } from 'vuex'
import { assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { listToTreeData } from '@/utils/index'
export default {
  components: { editDialog },
  data() {
    return {
      showPermDialog: false,
      defaultProps: {
        label: 'name'
      },
      permData: [], // 专门用来接收权限数据 树形数据
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      roleId: null, // 用来记录分配角色的id
      // 页码数组
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      // 接收获取到的数据数组
      list: [],
      // 接收获取到的公司数据对象
      formdata: {},
      showDialog: false,
      roleDetail: {}
    }
  },
  // 用辅助函数获取公共状态的值
  computed: {
    ...mapGetters(['companyId'])
  },
  // 获取初始数据
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 点击分配权限
    async assignPerm(id) {
      this.permData = listToTreeData(await getPermissionList(), '0')
      // console.log(124, this.permData)
      // 记住当前点击的id
      this.roleId = id
      // 根据id获取这个id的权限点
      const { permIds } = await getRoleDetail(id)
      console.log(128, permIds)
      this.showPermDialog = true
      // this.selectCheck = permIds
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
        // console.log(152, this.selectCheck)
      })
    },

    // 点击权限弹窗的确定
    async btnPermOK() {
      // console.log(137, this.$refs.permTree.getCheckedKeys())
      await assignPerm({
        permIds: this.$refs.permTree.getCheckedKeys(),
        id: this.roleId
      })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    },

    // 点击权限弹窗取消
    btnPermCancel() {
      // 清除数据
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys([])
        // console.log(152, this.selectCheck)
      }) // 重置数据
      this.showPermDialog = false
    },

    // 封装获取公司角色函数，后面要用到刷新，所以封装一下
    async getRoleList() {
      // 调用接口
      const { rows, total } = await getRoleList(this.page)
      // console.log(112, rows)
      this.list = rows
      this.page.total = total
    },

    // 点击页码后改变当前页
    changePage(newPage) {
      // console.log(112, newPage)
      this.page.page = newPage
      // 刷新页面
      this.getRoleList()
    },

    // 修改每页显示条数
    handleSizeChange(newPageSize) {
      // console.log(121, newPageSize)
      this.page.pagesize = newPageSize
      // 刷新页面
      this.getRoleList()
    },

    // 封装获取公司信息函数
    async getCompanyInfo() {
      const formdata = await getCompanyInfo(this.companyId)
      // console.log(115, formdata)
      this.formdata = formdata
    },

    // 删除功能的实现
    async deleteRole(id) {
      await this.$confirm('确定要删除该角色吗')
      // 调用接口
      await deleteRole(id)
      if (this.list.length <= 1 && this.page.page > 1) {
        this.page.page = this.page.page - 1
        await this.getRoleList(this.page)
      }
      this.$message.success('删除成功')
      this.getRoleList()
    },
    // 点击编辑显示弹框
    async editRole(id) {
      await this.$refs.editDialog.getRoleDetail(id)
      this.showDialog = true
    },
    // 点击新增添加弹窗
    addRole() {
      this.showDialog = true
    }
  }

}
</script>

<style>

</style>
